package com.gyms.service;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.util.Log;

import com.gyms.step.StepMode;

import click.StepCallBack;
import utils.DateUtil;
import utils.SPUtil;

/**
 * Created by ltx on 17/6/14
 * 计步传感器
 */

public class StepInPedometer extends StepMode  {
    private final String TAG = "StepInPedometer";
    private int liveStep = 0;
    //0-TYPE_STEP_DETECTOR 1-TYPE_STEP_COUNTER
    private int sensorMode = -1;  //0代表长时间计步 1代表短时间计步
    private String DB_NAME = "hula_gym_step";
    private StepService mService;
    private int mToadyBaseStep;//今天的起始步数
    private boolean mIsHaveTodayBaseStep;//是否含有今天的起始步数没有就去存储

    public StepInPedometer(Context context, StepCallBack stepCallBack) {
        super(context, stepCallBack);
        mService= (StepService) context;
        doInitToadyBaseStep();
    }

    /**
     * 初始化今天的起始步数
     */

    public void doInitToadyBaseStep() {
        String mTodayStr=DateUtil.getTodayDate();
        mToadyBaseStep= (int) SPUtil.get(mService, mTodayStr,-1);
        mIsHaveTodayBaseStep=mToadyBaseStep!=-1;
    }

    @Override
    protected void registerSensor() {
        addCountStepListener();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        liveStep = (int) event.values[0];

        if(sensorMode == 0){
            StepMode.CURRENT_SETP=doGetTodayStep(liveStep);
        }else if(sensorMode == 1){
            StepMode.CURRENT_SETP += liveStep;//每走一步加1
        }

        stepCallBack.Step(StepMode.CURRENT_SETP);

    }

    /**
     * 通过内置计步器获取今日步数
     * @param liveStep 系统返回的累积步数
     */

    private int doGetTodayStep(int liveStep) {
        int mTodayStep;
        if (mService.mTodayCurStep > liveStep || liveStep == 0) {//表示开机重启
            mTodayStep = liveStep + mService.mTodayCurStep;
        } else {
            if (!mIsHaveTodayBaseStep) {//没有基础步数就按0开始计算
                mToadyBaseStep = liveStep;
                mIsHaveTodayBaseStep = true;
                String mTodayStr = DateUtil.getTodayDate();
                SPUtil.put(mService, mTodayStr, mToadyBaseStep);
            }
            if(mToadyBaseStep>liveStep){
                return 0;
            }
            mTodayStep = liveStep - mToadyBaseStep;
        }
        return mTodayStep;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    private void addCountStepListener() {
        Sensor detectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        /*if (detectorSensor != null) {
            sensorManager.registerListener(this, detectorSensor, SensorManager.SENSOR_DELAY_UI);
            isAvailable = true;
            sensorMode = 0;
        } else if (countSensor != null) {
            sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
            isAvailable = true;
            sensorMode = 1;
        } else {
            isAvailable = false;
            Log.v(TAG, "Count sensor not available!");
        }*/
        if (countSensor != null) {
            sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
            isAvailable = true;
            sensorMode = 0;
        } else if (detectorSensor != null) {
            sensorManager.registerListener(this, detectorSensor, SensorManager.SENSOR_DELAY_UI);
            isAvailable = true;
            sensorMode = 1;
        } else {
            isAvailable = false;
            Log.v(TAG, "Count sensor not available!");
        }
    }
}
